<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-05-03
 * Time: 15:14
 */

namespace app\src\sunsun\aq806\action;


use app\src\base\action\BaseAction;
use app\src\sunsun\aq806\logic\Aq806PhHourHisLogic;
use app\src\sunsun\aq806\logic\VAq806PhDayHisLogic;

class VAq806PhHisAction extends BaseAction
{
    /**
     * 获取设备过去24小时的平均ph值
     * @param $did
     * @return array
     */
    public function oneDay($did)
    {
        $logic = (new Aq806PhHourHisLogic());
        $page = ['curpage' => 1, 'size' => 24];

        $result = $logic->query(['did' => $did], $page, 'ymdh desc');
        if ($result['status']) {
            $info = $result['info'];
            if (count($info['list']) > 0) {
                $list = $info['list'];
                $data = [];
                foreach ($list as $key => $vo) {
                    array_push($data, [
                        'his_date' => $vo['ymdh'],
                        'did' => $vo['did'],
                        'avg_ph' => $vo['ph']
                    ]);
                }
                //his_date, avg_ph, did
                return $this->success($data);
            }
        }

        return $this->success([]);
    }

    /**
     * 获取设备过去n天的平均ph值
     * @param $did
     * @param int $days
     * @return array
     */
    public function lastNDay($did, $days = 7)
    {
        $logic = (new VAq806PhDayHisLogic());
        $map = ['did' => $did];
        $ymd = date('Ymd', time() - $days * 24 * 3600);
        $map['his_date'] = ['egt', $ymd];
        $result = $logic->queryNoPaging($map, "his_date desc");

        return $this->result($result);

    }
}